/**
 * @file This file is used to execute some common actions.
 *       For example, set the skin of the page, etc.
 * @author www.sfg.name
 * @email 1991sfg@gmail.com
 */

/**
 * The global util object
 * 
 * @global
 * @type object
 */
var utils = {};

/**
 * Add a cookie key-value pair.
 * 在path=/下添加cookie
 * 
 * @param {string} key 名
 * @param {string||object||number|...} val 值
 * @param {number} expireDays 过期天数
 */
utils.setCookie = function (key, val, expireDays) {
  key = escape(key);
  val = escape(val);
  var dateTime = new Date();
  dateTime.setTime(dateTime.getTime() + expireDays * 24 * 60 * 60 * 1000);
  document.cookie = key + "=" + val + ";path=/;expires=" + dateTime;
};

/**
 * 拿到cookie中某个key对应的所有value
 * 
 * @param {string} name cookie的key
 * @returns {Array<string>} 一系列cookie
 */
utils.getCookies = function (name) {
  name = escape(name);
  
  var values = [];
  if(!document.cookie == ''){ 
    //用spilt('; ')切割所有cookie保存在数组arrCookie中 
    var arrCookie = document.cookie.split('; '); 
    var arrLength = arrCookie.length; 
    var keyValue;
    for(var i=0; i<arrLength; ++i) {
      keyValue = arrCookie[i].split('=');
      if (keyValue[0] == name) {
        values.push(unescape(keyValue[1]));
      }
    } 
  }
  return values; 
};

/**
 * 删除path=/下的某个cookie
 */
utils.deleteCookie = function (name) {
  name = escape(name);
  var exp = new Date();
  exp.setTime(exp.getTime() - 1000);
  document.cookie = name + "=;path=/;exp=" + exp;
};